﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace tsgl
{
    public partial class publisherInfoManage : Form
    {
        public publisherInfoManage()
        {
            InitializeComponent();
        }

        
        public static DataTable getPublisherInfo()
        {
            //return bookDbClass.createDataTableByKey("出版社编号,出版社名称,ISBN,出版社简称,出版社地址","出版社");
            DataTable dt = bookDbClass.ExecuteDataTable("select * from 出版社");
            return dt;
        }

        public static Boolean publisherInfoAdd(string publisherName,string ISBN,string shortName,string publisherAddress)
        {
            string strInsertComm = "insert into 出版社(出版社名称,ISBN,出版社简称,出版社地址) values({0},{1},{2},{3})";
            strInsertComm = string.Format(strInsertComm,publisherName,ISBN,shortName,publisherAddress);
            return bookDbClass.insertBySql(strInsertComm);
        }

        public static Boolean publisherInfoEdit(string publisherName, string ISBN, string shortName, string publisherAddress, string publisherID)
        {
            string strEditComm = "update 出版社 set 出版社名称={0},ISBN={1},出版社简称={2},出版社地址={3} where 出版社编号={4}";
            strEditComm = string.Format(strEditComm,publisherName,ISBN,shortName,publisherAddress,publisherID);
            return bookDbClass.editBySql(strEditComm);
        }

        public static Boolean publisherInfoDelete(string publisherID)
        {
            if (bookDbClass.deleteByKey("出版社", "出版社编号", publisherID) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void setEnable()
        {
            this.txtPublisherName.ReadOnly = false;
            this.txtISBN.ReadOnly = false;
            this.txtShortName.ReadOnly = false;
            this.txtAddress.ReadOnly = false;
        }

        private void initializeData()
        {
            DataTable dt = getPublisherInfo();
            this.dataGridView1.DataSource = dt.DefaultView;
            this.dataGridView1.Columns[0].Visible = false;
            this.dataGridView1.Columns[2].Width = 150;
            this.dataGridView1.Columns[4].Width = 200;
            this.txtPublisherId.DataBindings.Clear();
            this.txtPublisherName.DataBindings.Clear();
            this.txtISBN.DataBindings.Clear();
            this.txtShortName.DataBindings.Clear();
            this.txtAddress.DataBindings.Clear();
            this.txtPublisherId.DataBindings.Add("text",dt.DefaultView,"出版社编号");
            this.txtPublisherName.DataBindings.Add("text",dt.DefaultView,"出版社名称");
            this.txtISBN.DataBindings.Add("text",dt.DefaultView,"ISBN");
            this.txtShortName.DataBindings.Add("text",dt.DefaultView,"出版社简称");
            this.txtAddress.DataBindings.Add("text",dt.DefaultView,"出版社地址");
        }


        private void publisherInfoManage_Load(object sender, EventArgs e)
        {
            initializeData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.txtPublisherId.Visible = true;
            string publisherId = this.txtPublisherId.Text;      
            this.txtPublisherId.Visible = false;
            string publisherName = this.txtPublisherName.Text;
            string ISBN = this.txtISBN.Text;
            string shortName = this.txtShortName.Text;
            string address = this.txtAddress.Text;

            if (publisherId != "")
            {
                //publisherInfoEdit(publisherName, ISBN, shortName, address, publisherId);

                int i = Convert.ToInt32(publisherId);
                bookDbClass.ExecuteNonQurey("update 出版社 set ISBN=@a,出版社名称=@b,出版社简称=@c,出版社地址=@d where 出版社编号=@e", new SqlParameter("a", ISBN), new SqlParameter("b", publisherName), new SqlParameter("c", shortName), new SqlParameter("d", address), new SqlParameter("e", i));
                initializeData();
                MessageBox.Show("成功更新！", "提示信息");
            }
            else
            {
                //publisherInfoAdd(publisherName, ISBN, shortName, address);

                bookDbClass.ExecuteNonQurey("insert into 出版社(ISBN,出版社名称,出版社简称,出版社地址) values(@a,@b,@c,@d)", new SqlParameter("a", ISBN), new SqlParameter("b", publisherName), new SqlParameter("c", shortName), new SqlParameter("d", address));
                initializeData();
                MessageBox.Show("插入成功！", "提示信息");
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            initializeData();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除该条记录吗？", "删除提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                //if (publisherInfoDelete(this.txtPublisherId.Text.ToString()) == true)
                //{
                //    initializeData();
                //    MessageBox.Show("成功删除一条记录！", "提示信息");
                //}
                //else
                //{
                //    MessageBox.Show("删除记录失败！", "提示信息");
                //}

                if (bookDbClass.deleteByDt(this.dataGridView1.CurrentCell.RowIndex, "select * from 出版社") == true) 
                {
                    initializeData();
                    MessageBox.Show("成功删除一条记录！", "提示信息");
                }
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //int i = this.dataGridView1.CurrentCell.RowIndex;
        private void txtPublisherName_TextChanged(object sender, EventArgs e)
        {
            //int i = this.dataGridView1.CurrentCell.RowIndex;
            //this.dataGridView1.Rows[][1] = txtPublisherName.Text.Trim();            
        }

        private void txtISBN_TextChanged(object sender, EventArgs e)
        {
            //this.dataGridView1.Rows[this.dataGridView1.CurrentCell][2] = this.txtISBN.Text.Trim();
        }

        private void txtShortName_TextChanged(object sender, EventArgs e)
        {
            //this.dataGridView1.Rows[this.dataGridView1.CurrentCell][3] = this.txtShortName.Text.Trim();
        }

        private void txtAddress_TextChanged(object sender, EventArgs e)
        {
            //this.dataGridView1.Rows[this.dataGridView1.CurrentCell][4] = this.txtAddress.Text.Trim();
        }
    }
}